package stu.aistar.util;

import com.zhenzi.sms.ZhenziSmsClient;

import java.util.HashMap;
import java.util.Map;

public class CodeUtils {
    private static String apiUrl;
    private static String appId;
    private static String appSecret;

    public static String sendVerifyCode(String phoneNumber, Integer length, Map<String,String> map){
        String code = null;
        try {
            apiUrl = map.get("apiUrl");
            appId = map.get("appId");
            appSecret = map.get("appSecret");
            ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
            code = RandomUtils.getNumber(length);
            Map<String, Object> params = new HashMap<>();
            params.put("number", phoneNumber);
            params.put("templateId", map.get("templateId"));
            //这个参数就是短信模板上那两个参数
            String[] templateParams = new String[2];
            templateParams[0] = code;
            //模板上的参数--》时间
            templateParams[1] = map.get("timeOut");
            System.out.println(templateParams);
            params.put("templateParams", templateParams);
            String result = client.send(params);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return code;
    }
}
